<?php
/**
 * 语言包管理
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: LanguageService.class.php v1.0 2014-06-23 17:27:03 $
 */
namespace Admin\Service;

class LanguageService extends CommonService{
    /**
     * 添加语言数据
     * @param type $data
     * @return type
     */
    public function add($data) {
        $LangModel = $this->getD();
        if(false === ($lang = $LangModel->create($data))){
            return $this->errorResultReturn($LangModel->getError());
        }
        
        if(false === $LangModel->add($lang)){
            return $this->errorResultReturn("系统繁忙！");
        }
        return $this->resultReturn(TRUE);
    }
    
    /**
     * 更新语言数据
     * @param type $data
     * @return type
     */
    public function update($data) {
        $LangModel = $this->getD();
        if(false === ($lang = $LangModel->create($data))){
            return $this->errorResultReturn($LangModel->getError());
        }
        if(isset($lang['nid'])){
            unset($lang['nid']);
        }
        if(false === $LangModel->save($lang)){
            return $this->errorResultReturn("系统繁忙！");
        }
        return $this->resultReturn(TRUE);
    }
    
    /**
     * 删除语言
     * @param type $id
     * @return type
     */
    public function delete($id) {
        $where = array("is_system" => 0, "id" => $id);
        return $this->getD()->where($where)->delete();
    }
    /**
     * 检查模型名称是否可用
     * @param  string $title 模型名称
     * @param  int    $id   需要更新模型的id
     * @return array
     */
    public function checkTitle($title, $id) {
        $LangModel = $this->getD();
        $lang['title'] = trim($title);
        if ($LangModel->isValidTitle($lang, $id)) {
            return $this->resultReturn(true);
        }

        return $this->errorResultReturn($LangModel->getError());
    }
    /**
     * 检查语言是否可用
     * @param  array $langs 需要检查的语言
     * @param  int   $id    需要更新语言的id
     * @return array
     */
    public function checkLang($langs, $id) {
        $LangModel = $this->getD();
        if($id > 0){
            $resutl = $this->checkTitle($langs['title'], $id);
            if (false === $resutl['status']) {
                return $this->errorResultReturn($resutl['data']['error']);
            }
            return $this->resultReturn(true);
        }else{
            $lang = array_map('trim', $langs);
            
            if ($LangModel->isValid($lang, $id)) {
                return $this->resultReturn(true);
            }
        }
        return $this->errorResultReturn($LangModel->getError());
    }
    
    /**
     * 根据ID获取语言详情
     * @param type $id
     * @return type
     */
    public function getById($id) {
        return $this->getM()->getById($id);
    }
    /**
     * 获取所有语言
     * @return type
     */
    public function getAll(array $where, $field = "*") {
        return $this->getM()->field($field)->where($where)->select();
    }
    
    /**
     * 根据语言标识判断语言是否存在
     * @param type $nid
     */
    public function existLang($nid) {
        if ($this->getM()->where(array("nid" => $nid))->count() > 0) {
            return true;
        }
        return false;
    }
    /**
     * 根据语言ID判断语言是否存在
     * @param type $nid
     */
    public function existByIdLang($id) {
        if ($this->getM()->where(array("id" => $id))->count() > 0) {
            return true;
        }
        return false;
    }
    /**
     * 设置语言状态
     * @param  int   $id     语言id
     * @param  int   $status 语言状态
     * @return mixed
     */
    public function setStatus($id, $status) {
        return $this->getM()->where("id={$id}")->save(array('status' => $status));
    }
    /**
     * 获取本模块Model名称
     * @return string
     */
    protected function getModelName() {
        return "Language";
    }

}
